from os import X_OK, name
import re
from pymysql.cursors import Cursor
from requests.api import options
import urllib3
import requests
from selenium import webdriver
from time import sleep
import pymysql
from lxml import etree
urllib3.disable_warnings()
class trainingDetail:
    userr='verus'    #账号
    passwdd='a15938381591' #密码
    start_url='https://www.ubaike.cn/' #开始链接
    login_url = 'https://www.ubaike.cn/index.html' #登录页链接
    login_xpath='/html/body/div[1]/div/div/nav[2]/span/a[1]' #登录页xpath
    login_xpath2='//*[@id="login-show1"]/h6' #登录页xpath2  如果有
    login_user_xpath='//*[@id="xm-login-user-name"]'  #登录名xpath
    login_passwd_xpath='//*[@id="xm-login-user-password"]' #登录密码xpath
    login_button_xpath='//*[@id="login_submit"]' #登录按钮xpath
    #获取cookie后登录的页面
    cookie_url='https://www.ubaike.cn/'
    #数据库
    host='192.168.0.113'
    port=3306
    user='coname'
    passwd='xZS6p4LDcKCwn4Yb'
    charset='utf8'
    db='co'
    #寻找查询输入框
    find_xpath='//*[@id="searchkey"]'#输入框界面
    find_button='//*[@id="indexSearchForm"]/div/span/input'#输入框按钮
    #机构名称目录
    trains_dir='/html/body/div[4]/div/div[2]/div/div[2]/ul[2]/li'
    

    def __init__(self):
        #初始化浏览器
        options=webdriver.ChromeOptions()
        options.add_experimental_option("excludeSwitches",["enable-logging"])
        self.bro= webdriver.Chrome(options=options)
        self.bro.maximize_window()
        self.resus=[]
        self.recus=[]
        sleep(1)

    def train_login(self):#点击登录获取cookie
        #进入还有登录页面
        self.bro.get(self.login_url)
        sleep(1)
        self.bro.current_window_handle
        #进入登录入口
        login=self.bro.find_element_by_xpath(self.login_xpath)
        self.bro.execute_script("arguments[0].click();",login)
        sleep(1)
        self.bro.current_window_handle
        #进入登录入口2  如果有的话
        login2= self.bro.find_element_by_xpath(self.login_xpath2)
        self.bro.execute_script('arguments[0].click();',login2)
        sleep(1)
        self.bro.current_window_handle
        #登录账号
        self.bro.find_element_by_xpath(self.login_user_xpath).send_keys(self.userr)
        sleep(1)
        #登录密码
        self.bro.find_element_by_xpath(self.login_passwd_xpath).send_keys(self.passwdd)
        sleep(1)
        #点击登录
        button = self.bro.find_element_by_xpath(self.login_button_xpath)
        self.bro.execute_script("arguments[0].click();",button)
        sleep(1)
        self.bro.current_window_handle
        cookies=self.bro.get_cookies()
        return cookies

    def train_get_cookie(self):#进入到详情页
        cookies=self.train_login()
        req=requests.Session()
        for cookie in cookies:
            req.cookies.set(cookie['name'],cookie['value'])
        response = req.get(self.cookie_url,verify=False)
        return response

    def train_titl(self):
        self.train_get_cookie()
        conn,cursor= self.open_datas()#打开数据库
        resus,recus= self.output_datas(conn,cursor)#取出数据
        self.close_datas(conn,cursor)#关闭数据库
        return resus,recus

    def open_datas(self):#打开数据库
        try:
            self.conn=pymysql.Connection(host=self.host,port=self.port,user=self.user,passwd=self.passwd,db=self.db,charset=self.charset)
            self.cursor=self.conn.cursor()
            print("打开数据库成功!")
            return self.conn,self.cursor
        except Exception as e:
            print("打开数据库失败>>>",e)

    def output_datas(self,conn,cursor):#取出数据库
        sql='select srname,fileurl from co_training'
        cursor.execute(sql)
        resutls = cursor.fetchall()
        for result in resutls:
            resul=''.join(result[0])
            self.resus.append(resul)
            resull=''.join(result[1])
            self.recus.append(resull)
        return self.resus,self.recus
        
    def input_datas(self):#存入数据库
        resus,recus=self.train_titl()
        for i in range(len(resus)):
            #输入机构名称
            # self.bro.find_element_by_xpath(self.find_xpath).clear()
            # self.bro.find_element_by_xpath(self.find_xpath).send_keys(resu)
            # sleep(1)
            # button =  self.bro.find_element_by_xpath(self.find_button)
            # self.bro.execute_script("arguments[0].click();",button)
            # self.bro.current_window_handle
            # sleep(4)
            #获取cookie
            req=requests.Session()
            cookies=self.bro.get_cookies()
            for cookie in cookies:
                req.cookies.set(cookie['name'],cookie['value'])
            #获取html
            # print(self.bro.current_url)
            resp= etree.HTML(req.get(self.bro.current_url,verify=False).text)
        #获取详情页url
             #               
            # try:
            #     detail_url=resp.xpath("//div[@class='content']/a/@href")[0]
            # except Exception as e:
            #     continue
            # print(detail_url)
            #获取机构详细信息
            self.detail(recus[i],resus[i])

            # self.bro.back()
            self.bro.current_window_handle

    def detail(self,url,srname):#进入到详情页   详情页的xpath因为网站不一样  不做公共变量了
        data_details=[]
        data_titles=[]
     
        req=requests.Session()
        print(srname)
        try:
            cookies=self.bro.get_cookies()
            for cookie in cookies:
                req.cookies.set(cookie['name'],cookie['value'])
            #获取html
            resp= etree.HTML(req.get(url,verify=False).text)
            xboss=resp.xpath('//*[@id="author-area"]/div/div/span/a//text()')
            print(xboss)
            content_items=resp.xpath('//*[@id="partners"]/div[@class="basic-item"]')
        except Exception as e:
            print('获取页面的初始错误>>>',e)
        try:
            for content_item in content_items:
                content_left=content_item.xpath(".//div[@class='basic-item-left']//text()")
                content_right=content_item.xpath(".//div[@class='basic-item-right']//text()")
                try:
                    if content_left[0]=='曾用名':
                        continue
                    elif content_left[0]=='核准日期':
                        continue
                    elif content_left[0]=='登记机关':
                        continue
                    elif content_left[0]=="":
                        continue
                    else:
                        data_details.append(content_right[0])
                        data_titles.append(content_left[0])
                except Exception as e:
                    print("取出数据出现问题>>>",e)
                    continue
        except Exception as e:
            print('获取信息错误>>>',e)
        try:
            if data_titles[0] != '企业名称':
                data_titles.insert(0,'企业名称')
                data_details.insert(0,srname)
            if data_titles[1] !='法定代表人':
                data_titles.insert(1,'法定代表人')
                data_details.insert(1,xboss)
            print(data_details)
        except Exception as e:
            print("查看信息错误>>>",e)
        
        #获取下面的股东信息
        # mations=resp.xpash("/html/body/div[4]/div/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[@class='content-block']")
        # for mation in mations:
        #     if mation.xpath('.//@id')[0]=='partners':
        #         continue
        #     else:
        #         mation[0].xpath('string(.)').strip()
        #         data_contents.append(mation[0].xpath('string(.)').strip())
            # contentsss=resp.xpath("//div[@class='article']")
            # contentss1=contentsss[0].xpath('string(.)').strip()
            # contents=contentss1.replace('\n','')
            # contents2=contents.replace('\r','')
            # contentss=contents2.strip()
            # print(srname)

            # email=re.findall('邮箱：(.*?)地址',contentss)[0]
            # address=re.findall('地址：(.*?)工商',contentss)[0]
            # boss=re.findall('法定代表人(.*?)注册号',contentss)[0]
            # regist_card=re.findall('注册号(.*?)统一社会信用代码',contentss)[0]
            # loney_card=re.findall('统一社会信用代码(.*?)注册资本',contentss)[0]
            # regist_base=re.findall('注册资本(.*?)成立日期',contentss)[0]
            # create_date=re.findall('成立日期(.*?)企业类型',contentss)[0]
            # company_type=re.findall('企业类型(.*?)经营范围',contentss)[0]
            # try:
            #     scope_of_business=re.findall('经营范围(.*?)公司住所',contentss)[0]
            # except Exception as e:
            #     print("公司住所为注册地址》》》",e)
            #     scope_of_business=re.findall('经营范围(.*?)注册地址',contentss)[0]
            # try:
            #     regist_address=re.findall('公司住所(.*?)营业期限',contentss)[0]
            # except Exception as e:
            #     print("公司住所为注册地址》》》",e)
            #     regist_address=re.findall('注册地址(.*?)营业期限',contentss)[0]
            # time_of_business=re.findall('营业期限(.*?)登记机关',contentss)[0]
            # try:
            #     registration = re.findall('登记机关(.*?)经营状态',contentss)[0]
            # except Exception as e:
            #     print("登记机关不存在>>>",e)
            #     pass
            # status_of_business=re.findall('经营状态(.*?)股东信息',contentss)[0]
            # shareholders=re.findall('股东信息(.*?)主要人员',contentss)[0]
            # the_mian=re.findall('主要人员(.*?)变更记录',contentss)[0]
            # the_change=re.findall('变更记录(.*?)分享到',contentss)[0]
            #,srshareholder="{}",srmain="{}",srchange="{}" ,shareholders,the_mian,the_change

            # 录入信息进数据库
        conn,cursor=self.open_datas()
        cursor.execute('update co_training set fileurl="{}",srname="{}",srboss="{}",srregistcard="{}",srloneycard="{}",srregistbase="{}",srcreatedate="{}",srcompanytype="{}",srscopeofbusiness="{}",srregistaddress="{}",srtimeofbusiness="{}",srstatusofbusiness="{}" where srname="{}"'.format(url,data_details[0],data_details[1],data_details[2],data_details[3],data_details[4],data_details[5],data_details[6],data_details[7],data_details[8],data_details[9],data_details[10],data_details[0]))
        conn.commit()
        print("更新数据成功")
        self.close_datas(conn,cursor)

        # except Exception as e:
        #     print("访问详情页失败>>>",e)
            

    def close_datas(self,conn,cursor):#关闭数据库
        try:
            cursor.close()
            conn.close()
            print("关闭数据成功!")
        except Exception as e:
            print("关闭数据库失败>>>",e)

    def train_close(self):#关闭浏览器
        self.bro.quit()

    def train_run(self):
        self.input_datas()
        sleep(1)


if __name__ == "__main__":
    trainingDetailer=trainingDetail()
    trainingDetailer.train_run()
    